<!--
 * @Description: 编辑门店
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2021-07-03 11:41:05
 * @LastEditTime: 2023-05-15 15:53:35
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
-->

<template>
  <div class="lb-store-manage-edit">
    <top-nav :title="navTitle" :isBack="true" />
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="130px"
      >
	  <el-form-item label="标题" prop="name">
	    <el-input
	      v-model="subForm.name"
	
	      show-word-limit
	      placeholder="请输入标题"
	    ></el-input>
	  </el-form-item>
 
		<el-form-item label="排序" prop="order_id">
         <el-input
           v-model="subForm.order_id"
           placeholder="请输入排序"
         ></el-input>
       </el-form-item>
       
        <el-form-item>
          <lb-button type="primary" @click="submitForm" v-preventReClick>{{
            $t('action.submit')
          }}</lb-button>
          <lb-button @click="$router.back(-1)">{{
            $t('action.back')
          }}</lb-button>
        </el-form-item>
      </el-form>

      <lb-map
        :dialogVisible.sync="showMap"
        @selectedLatLng="getLatLng"
      ></lb-map>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      navTitle: '',
      showMap: false,
      base_agent: [],
      subForm: {
        id: 0,
        name: '',
		order_id:''
      
      },
      subFormRules: {
        name: { required: true, validator: this.$reg.isNotNull, text: '标题', reg_type: 2, trigger: 'blur' },
      }
    }
  },
  async created () {
    let { id } = this.$route.query
    await this.getBaseInfo()
    if (id) {
      this.subForm.id = id
      this.getDetail(id)
    }
    this.navTitle = this.$t(id ? 'menu.ActiontypeEdit' : 'menu.ActiontypeAdd')
  },
  methods: {
    async getBaseInfo () {
      let { code, data } = await this.$api.agent.adminSelect()
      if (code !== 200) return
      this.base_agent = data
    },
    /**
     * @name: 详情
     * @param {*} id
     */
    async getDetail (id) {
      let { code, data } = await this.$api.live.ActiontypeInfo({ id })
      if (code !== 200) return
      data.cover = [{ url: data.cover }]
		data.cover_svg = [{ url: data.cover_svg }]
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    getCover (img, key) {
      this.subForm[key] = img
    },
    getLatLng (latLng) {
      this.subForm.lat = latLng.lat
      this.subForm.lng = latLng.lng
    },
    /**
     * @name: 新增/编辑
     * @param {*}
     */
    submitForm () {
      let flag = true
      this.$refs['subForm'].validate(valid => {
        if (!valid) flag = false
      })
      if (flag) {
        let subForm = JSON.parse(JSON.stringify(this.subForm))
    
        let methodModel = subForm.id ? 'ActiontypeUpdate' : 'ActiontypeAdd'
		
        this.$api.live[methodModel](subForm).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t(subForm.id ? 'tips.successRev' : 'tips.successSub'))
            this.$router.back(-1)
          }
        })
      }
    }
  },
  filters: {
    handleStartEndTime (val) {
      let text = ''
      let { start_time: start, end_time: end } = val
      let day = moment(Date.now()).format('YYYY-MM-DD')
      if (start && end && (moment(`${day} ${end}`).unix() < moment(`${day} ${start}`).unix())) {
        text = '次日'
      }
      return text
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-store-manage-edit {
  width: 100%;
  .el-form {
    width: 100%;
    .el-select,
    .el-input-number,
    .el-input {
      width: 300px;
    }
    .el-textarea {
      width: 600px;
    }
  }
}
</style>
